package com.photoeditor.demo.ui.widget.image.edit;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.photoeditor.R;
import com.photoeditor.demo.ui.widget.image.AnimationCropImageView;
import com.photoeditor.demo.ui.widget.image.CustomTabCheckBg;

public class CropBarView extends RelativeLayout implements IDealInit, View.OnClickListener {

    private AnimationCropImageView mImageView;

    private CustomTabCheckBg mBtnCustom;
    private CustomTabCheckBg mBtn1_1;
    private CustomTabCheckBg mBtn4_5;
    private CustomTabCheckBg mBtn4_3;
    private CustomTabCheckBg mBtn2_1;
    private CustomTabCheckBg mBtn2_3;
    private CustomTabCheckBg mBtnCover;
    private CustomTabCheckBg mBtn16_9;
    private CustomTabCheckBg mBtn9_16;
    private CustomTabCheckBg mBtn3_4;

    private LinearLayout mParentView;


    public CropBarView(Context context) {
        this(context, null);
    }

    public CropBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mParentView = (LinearLayout) findViewById(R.id.main_layout);

        mBtnCustom = (CustomTabCheckBg) findViewById(R.id.crop_custom);
        mBtn1_1 = (CustomTabCheckBg) findViewById(R.id.crop_1_1);
        mBtn4_5 = (CustomTabCheckBg) findViewById(R.id.crop_4_5);
        mBtn4_3 = (CustomTabCheckBg) findViewById(R.id.crop_4_3);
        mBtn2_1 = (CustomTabCheckBg) findViewById(R.id.crop_2_1);
        mBtn2_3 = (CustomTabCheckBg) findViewById(R.id.crop_2_3);
        mBtnCover = (CustomTabCheckBg) findViewById(R.id.crop_cover);
        mBtn16_9 = (CustomTabCheckBg) findViewById(R.id.crop_16_9);
        mBtn9_16 = (CustomTabCheckBg) findViewById(R.id.crop_9_16);
        mBtn3_4 = (CustomTabCheckBg) findViewById(R.id.crop_3_4);

        mBtnCustom.setOnClickListener(this);
        mBtn1_1.setOnClickListener(this);
        mBtn4_5.setOnClickListener(this);
        mBtn4_3.setOnClickListener(this);
        mBtn2_1.setOnClickListener(this);
        mBtn2_3.setOnClickListener(this);
        mBtnCover.setOnClickListener(this);
        mBtn16_9.setOnClickListener(this);
        mBtn9_16.setOnClickListener(this);
        mBtn3_4.setOnClickListener(this);
    }

    @Override
    public void init() {
        mBtnCustom.setChecked(true);
        mImageView.setFixedAspectRatio(false);
    }

    public void setAnimationCropImageView(AnimationCropImageView view) {
        mImageView = view;
    }

    @Override
    public void onClick(View v) {
        for (int i = 0; i < mParentView.getChildCount(); i++) {
            ((CustomTabCheckBg) mParentView.getChildAt(i)).setChecked(false);
        }

        switch (v.getId()) {
            case R.id.crop_custom:
                mBtnCustom.setChecked(true);
                mImageView.setFixedAspectRatio(false);
                break;
            case R.id.crop_1_1:
                mBtn1_1.setChecked(true);
                mImageView.setAspectRatio(1, 1);
                mImageView.setFixedAspectRatio(true);
                break;
            case R.id.crop_4_5:
                mBtn4_5.setChecked(true);
                mImageView.setAspectRatio(4, 5);
                mImageView.setFixedAspectRatio(true);
                break;
            case R.id.crop_4_3:
                mBtn4_3.setChecked(true);
                mImageView.setAspectRatio(4, 3);
                mImageView.setFixedAspectRatio(true);
                break;
            case R.id.crop_2_1:
                mBtn2_1.setChecked(true);
                mImageView.setAspectRatio(2, 1);
                mImageView.setFixedAspectRatio(true);
                break;
            case R.id.crop_2_3:
                mBtn2_3.setChecked(true);
                mImageView.setAspectRatio(2, 3);
                mImageView.setFixedAspectRatio(true);
                break;
            case R.id.crop_cover:
                mBtnCover.setChecked(true);
                mImageView.setAspectRatio(820, 312);
                mImageView.setFixedAspectRatio(true);
                break;
            case R.id.crop_16_9:
                mBtn16_9.setChecked(true);
                mImageView.setAspectRatio(16, 9);
                mImageView.setFixedAspectRatio(true);
                break;
            case R.id.crop_9_16:
                mBtn9_16.setChecked(true);
                mImageView.setAspectRatio(9, 16);
                mImageView.setFixedAspectRatio(true);
                break;
            case R.id.crop_3_4:
                mBtn3_4.setChecked(true);
                mImageView.setAspectRatio(3, 4);
                mImageView.setFixedAspectRatio(true);
                break;
        }
    }
}
